<?php
/**
 * Created by PhpStorm.
 * User: 秋刀鱼
 * Date: 2018/7/13
 * Time: 13:32
 */

namespace app\models;

use yii\base\Model;
use Yii;
use \common\helps\tools;//使用工具类

class Depart extends Model
{
    //表名称
    protected $table = '';


    /**
     * title 权限
     * @param $userId
     * @return int
     */
    public function auth($userId){
        $query = new \yii\db\Query();
        //权限
        $t_s_user_org = $query->select(['ID','user_id','org_id'])->from('ifssc_php.t_s_user_org')->where('user_id',$userId)->all();
        foreach($t_s_user_org as $ko => $vo){
            //单位 财务 排除
            if($vo['role_id'] != 1 || $vo['role_id'] != 4){
                //获取部门下的所有成员
                $ApiDepart = new ApiDepart($this->connection);
                $getDeparAlltUsers = $ApiDepart->getDeparAlltUsers($vo['org_id']);

                foreach($getDeparAlltUsers as $kr => $kv){
                    //一通全通
                    if($kv['ID'] == $userId){
                        $this->pass = 1;
                    }

                }
            }
        }
        if($this->pass != 1){
            return 1;
        }
    }

    /**
     * title 获取部门下所有成员
     * @param null $departId
     * @return string
     */
    public function getDeparAlltUsers($departId = null){
        $depart = db('t_s_depart',$this->connection,false)->field('ID,org_type,departname,parentdepartid')->select();
        $getChildDeparts = $this->getChildDeparts($depart,$departId);
        $t_s_depart = db('t_s_depart',$this->connection,false)->field('ID,org_type,departname,parentdepartid')->where('ID',$departId)->select();
        $getChildDeparts = array_merge($t_s_depart,$getChildDeparts);
        //获取部门id
        $ids = '';
        foreach($getChildDeparts as $vd){
            $ids .=$vd['ID'].',';
        }

        if(count($getChildDeparts) == 1 || (count($getChildDeparts) < 1)){
            $ids .=$vd['ID'];
        }
        if (isset($departId)) {
            $usersArr = db('t_s_user_org', $this->connection, false)
                ->alias('o')
                ->join('t_s_base_user b', 'b.ID = o.user_id')
                ->join('t_s_user u', 'u.id = b.ID')
                ->where("o.org_id", 'in',$ids)
                ->where("org_id", 'in',$ids)
                ->field("u.account_no,u.account_name,u.account_bank,u.account_province,u.account_province,u.account_cnaps,b.realname,b.username,b.ID")
                ->select();
            return $usersArr;
        }
        else{
            return "the departId can't be null";
        }



    }



}